package com.docker.apps.afterservice.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.apps.R;
import com.docker.apps.afterservice.vm.AfterServiceViewModel;
import com.docker.apps.databinding.ProOrderRefundActivityBinding;
import com.docker.apps.order.utils.TimeLineItemDecoration;
import com.docker.apps.order.vo.OrderRefundVo;
import com.docker.apps.order.vo.RefundOrderVo;
import com.docker.common.common.command.NitDelegetCommand;
import com.docker.common.common.model.CommonListOptions;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.ui.base.NitCommonActivity;
import com.docker.common.common.ui.base.NitCommonFragment;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.utils.rxbus.RxBus;
import com.docker.common.common.utils.rxbus.RxEvent;
import com.docker.common.common.vm.NitCommonListVm;
import com.docker.common.common.vo.UserInfoVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * kxf -> 2020-02-19
 **/
@Route(path = AppRouter.ORDER_REFUND_PROCESS)
public class OrderRefundProcessActivity extends NitCommonActivity<AfterServiceViewModel, ProOrderRefundActivityBinding> {


    @Override
    protected int getLayoutId() {
        return R.layout.pro_order_refund_activity;
    }

    @Override
    public AfterServiceViewModel getmViewModel() {
        return ViewModelProviders.of(this, factory).get(AfterServiceViewModel.class);
    }

    @Override
    protected void inject() {
        super.inject();
        ARouter.getInstance().inject(this);
    }


    @Override
    public void initView() {
//        OrderVoV2 orderVoV2 = (OrderVoV2) getIntent().getSerializableExtra("order");
        String orderId = getIntent().getStringExtra("orderid"); //orderVoV2.refundsNo;
        String refundsNo = getIntent().getStringExtra("refundsNo"); //orderVoV2.refundsNo;
        if (TextUtils.isEmpty(orderId)) {
            ToastUtils.showShort("数据不正确");
            finish();
        }
        mToolbar.setTitle("退款/取消进度");
        mBinding.refundT1.setText(String.format("订单编号   %s", refundsNo));
        mViewModel.mCommonListReq = new CommonListOptions();
        mViewModel.mCommonListReq.falg = 4;
//        mViewModel.loadData();
        mBinding.refundR1.addItemDecoration(new TimeLineItemDecoration());
        mBinding.setVariable(com.docker.apps.BR.viewmodel, mViewModel);
        UserInfoVo userInfoVo = CacheUtils.getUser();
        HashMap<String, String> params = new HashMap<>();
        params.put("memberid", userInfoVo.uid);
        params.put("uuid", userInfoVo.uuid);
        params.put("refundsNo", refundsNo);
        mViewModel.getProcessRefund(params);
        mBinding.tvLookWl.setOnClickListener(v -> {
            v.setVisibility(View.GONE);
            params.put("orderid", orderId);
            mViewModel.cancelTui(params);
        });
    }

    @Override
    public void initObserver() {
        mViewModel.mServerLiveData.observe(this, o -> {
            if (o instanceof RefundOrderVo) {
                RefundOrderVo refundOrderVo = (RefundOrderVo) o;
                mViewModel.setRefundVos(refundOrderVo.getLog());
                runOnUiThread(() -> {
                    mBinding.refundT6.setText(String.format("[%s]", refundOrderVo.getStrStatus()));
                    mBinding.refundT4.setText(String.format("退款金额    %s", refundOrderVo.getTotal()));
                    mBinding.refundT5.setText(String.format("退款方式    %s", "原路返回"));
                    if (refundOrderVo.getStatus().equals("0"))
                        mBinding.tvLookWl.setVisibility(View.VISIBLE);
                    mBinding.refundT6.setTextColor(Color.parseColor("#F9AE26"));
                    if (refundOrderVo.getStatus().equals("2")) {
                        mBinding.tvLookWl.setVisibility(View.GONE);
                        mBinding.refundL1.setVisibility(View.VISIBLE);
                        mBinding.refundT6.setTextColor(Color.parseColor("#FF453A"));
                    }
                });
            } else {
                RxBus.getDefault().post(new RxEvent<>("refresh_buy", ""));
                ToastUtils.showShort("撤销成功");

            }
        });


    }

    @Override
    public NitDelegetCommand providerNitDelegetCommand(int flag) {

        NitDelegetCommand nitDelegetCommand = null;

        nitDelegetCommand = new NitDelegetCommand() {
            @Override
            public Class providerOuterVm() {
                return AfterServiceViewModel.class;
            }

            @Override
            public void next(NitCommonListVm commonListVm, NitCommonFragment nitCommonFragment) {
                ((AfterServiceViewModel) commonListVm).flag = flag;
            }
        };
        return nitDelegetCommand;

    }

    @Override
    public void initRouter() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}




